package group.rober.jeki.admin.controller

import group.rober.auth.service.AuthService
import group.rober.jeki.entity.Domain
import group.rober.jeki.entity.Tag
import group.rober.jeki.service.DomainService
import group.rober.saber.kit.NumberKit
import group.rober.saber.kit.StringKit
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.ModelAttribute
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/admin")
class AdminDomainController {
    @Autowired
    DomainService domainService;

    @RequestMapping("/domain-list")
    ModelAndView domainTag() {
        List<Domain> domainList = domainService.getDomainList();
        Map<String,Object> vars = new HashMap<String,Object>();
        return new ModelAndView("views/admin/domain-list",[
                "domainList":domainList
                ,"activeSlideItem":"domain"
        ]);
    }
    @RequestMapping("/domain-create")
    ModelAndView domainCreate() {
        Domain domain = new Domain();
        return new ModelAndView("views/admin/domain-editor",["domain":domain]);
    }
    @RequestMapping("/domain/{domainId}")
    ModelAndView domainEditor(@PathVariable("domainId") String domainId) {
        Domain domain = domainService.getDomain(domainId);
        List<Tag> inDomainTagList = domainService.getInDomainTagList(domainId);
        List<Tag> outDomainTagList = domainService.getOutDomainTagList(domainId);
        return new ModelAndView("views/admin/domain-editor",[
                "domain":domain
                ,"inDomainTagList":inDomainTagList
                ,"outDomainTagList":outDomainTagList
        ]);
    }
    @RequestMapping("/domain/delete-action/{domainId}")
    ModelAndView domainDelete(@PathVariable("domainId") String domainId) {
        Domain domain = domainService.getDomain(domainId);
        domainService.deleteDomain(domain);
        return new ModelAndView("redirect:/admin/domain-list");
    }
    @RequestMapping(path="/domain/save-action",method = RequestMethod.POST)
    ModelAndView domainSaveAction(@ModelAttribute Domain dataDomain) {
        if(StringKit.isBlank(dataDomain.getId())){
            dataDomain.setId(NumberKit.nanoTime36());
        }
        domainService.saveDomain(dataDomain);
        return new ModelAndView("redirect:/admin/domain-list");
    }

    @RequestMapping("/domain/{domainId}/add-tag/{tagId}")
    ModelAndView domainAddTag(@PathVariable("domainId") String domainId,@PathVariable("tagId") String tagId) {
        domainService.addTagToDomain(tagId,domainId)
        return new ModelAndView("redirect:/admin/domain/"+domainId);
    }
    @RequestMapping("/domain/{domainId}/remove-tag/{tagId}")
    ModelAndView domainRemoveTag(@PathVariable("domainId") String domainId,@PathVariable("tagId") String tagId) {
        domainService.removeTagToDomain(tagId,domainId);
        return new ModelAndView("redirect:/admin/domain/{domainId}");
    }
}
